<?php
namespace app\common\model;
use think\Model;
use think\Db;
use app\common\model\Redis;
use app\common\model\Dev_data_info;
use app\common\model\Wechat;
use app\common\model\Aes;
class Company_dev extends Model
{

	/**
	 * 获取单条设备信息
	 * @param  [array] $where [数组]
	 * @return [type]        [description]
	 */
	public function getOne($where,$field='*'){
        $result = self::field($field)->where($where)->find();
        if($result){
            $result = $result->getData();
        }
        return $result;
	}

    /**
     * 获取单条设备信息
     * @param  [array] $where [数组]
     * @return [type]        [description]
     */
    public function getDevAll($where=[],$order,$field='*',$pageSize=15,$pageParam=[]){

        $result = self::field($field)->order($order);
        if($where){
            $result = $result->where($where);
        }
        $result = $result->paginate($pageSize,false,$pageParam);
        return $result;
    }

    /**
     * 获取设备列表
     * @param int $end 结束时间
     * @return array
     * */
    public function devAll($where=[],$field="*"){
        $result = self::field($field);
        
        if ($where){
            $result->where($where);
        }
        $result = $result->select();

        return $result;
    }


	/**
     * 更改
     * @param $id
     * @param $data
     * @return $this
     */
    public function updInfo($where,$data){
        return self::where($where)->update($data);
    }

    /**
     * 查看设备是否存在
     * @param  [string] $guid1 [设备号]
     * @return [bool]        [true:不存在 false:存在]
     */
    public function guidOnly($guid1){
        $res = self::where(['guid1'=>$guid1])->field("id")->find();
        if($res){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 企业申请单个设备
     * @param [string] $uid   [用户id]
     * @param [string] $devid [设备号]
     */
    public function addDevs($uid,$devid){
        
        $guid2 = $this->createguid2();
        $vcode = $this->createguid2(16);
        $key = $this->createguid2(16);

        $add["guid1"] = $devid;
        $add["guid2"] = $guid2;
        $add["uid"] = $uid;
        $add["vcode"] = $vcode;
        $add["key"] = $key;
        $add["a_time"] = time();

        return self::insert($add);
    }

    /**
     * 申请设备(输入数量)
     * @param  [string] $num [数量]
     * @param  [string] $uid [用户id]
     * @return [array]      [申请的设备id]
     */
    public function numapply($uid,$num){
        $data = array();
        for($i=0;$i<$num;$i++){
            $guid1 = $this->createguid1();
            $guid2 = $this->createguid2();
            $vcode = $this->createguid2(16);
            $key = $this->createguid2(16);

            $add["guid1"] = $guid1;
            $add["guid2"] = $guid2;
            $add["uid"] = $uid;
            $add["vcode"] = $vcode;
            $add["key"] = $key;
            $add["a_time"] = time();
            
            $data[$i] = $guid1;
            self::insert($add);
        }
        return $data;
    }

    /**
     * 生成guid1 且保证唯一性,且只有小写字母与数字
     * @param string $start [前2位字符,可为空]
     * @return [string] $str [设备id号]
     */
    public function createguid1($start=NULL,$length=4){

       $length = config("GUID1_LEN");
       $strPol = "123456789abcdefghiljkmnpqrstuvwxyz";
       $max = strlen($strPol)-1;

       if($start){
           $str = $start;
       }else{
           $str = "";
           $length = 6;
       }

       for($i=0;$i<$length;$i++){
        $str.=$strPol[rand(0,$max)];
       }

       $return = $this->guidOnly($str);
       if(!$return){
            return $this->createguid1();
       }
       return $str;
    }

    /**
     * 生成guid2/vcode
     * @param  [string] $length [ 长度,默认为6]
     * @return [string] $str   [guid2/vcode]
     */
    public function createguid2($length=6){
       $str = "";
       $strPol = "123456789abcdefghiljkmnpqrstuvwxyz";
       $max = strlen($strPol)-1;

       for($i=0;$i<$length;$i++){
        $str.=$strPol[rand(0,$max)];
       }
       
       return $str;
    }


    /**
     * 生成设备微信二维码
     * @param  [string] $dev_id [设备号]
     * @return [array]         [code:true false  msg:图片路径]
     */
    public function createCode($dev_id,$path=""){
        //查看是否已经存在
        if(!$path){
            $path = config("WETCHAT_CODE");
        }
        
        $filename = $path.$dev_id.".jpg";
        if(file_exists($filename)){
            $data = array(
                 "code"=>true,
                 "msg"=>$filename
            );
        }else{
            $weixin = new Wechat();
            $return = $weixin->createCode($dev_id);
            if($return["code"]){
                $data = array(
                     "code"=>true,
                     "msg"=>$return["msg"]
                );
            }else{
                $data = array(
                     "code"=>false,
                     "msg"=>"生成失败"
                );
            }
        }
        return $data;
    }


    /**
     * 下载微信二维码到本地
     * @param  [string] $dev_id [设备号]
     */
    public function downWchatCode($dev_id){
            
        $path = config("WETCHAT_CODE");
        $filename = $path.$dev_id.".jpg";

        if(file_exists($filename)){
            //存在就下载到本地
            download_by_path($filename);
        }else{
            //不存在则生成后再下载
            $return = $this->createCode($dev_id);
            if($return["code"]){
                download_by_path($filename);
            }
        }
    }

    /**
     * 生成APP二维码
     * @param  [string] $dev_id [设备id]
     * @param  [boolean] $type 类型:true 浏览器下载,false 页面查看
     * @param [string] $path 服务器存放路径
     */
    public function createAppCode($dev_id, $type, $path=''){
        //不存在,则生成
        $keyid = rand(0,100);
        $aesModel = new Aes();
        //秘钥
        $key = $aesModel->createKey($keyid);

        $data = $dev_id."_".config("AES_DATA_STRING");
        $load = $aesModel->aesTwoCode($key,$data);

        //$url = "http://".$_SERVER['SERVER_NAME']."/swaylink.php/King/Index/appDownload/load/".$load."/id/".$keyid;
        
        $url = "King/Index/appDownload/load/".$load."/id/".$keyid;
        
        
        barQrCode($dev_id, $url, $type, $path);
    }

}